﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace yoyo
{
    public class IO
    {
        /// <summary>
        /// 更新本地配置文件
        /// </summary>
       public static void UpdateLocalVersionFile(Dictionary<string,string> ServerResVersion)
        {
            StringBuilder versions = new StringBuilder();
            foreach (var item in ServerResVersion)
            {
                versions.Append(item.Key).Append(",").Append(item.Value).Append("\n");
            }

            FileStream stream = new FileStream(GlobalFile.UnityPath + GlobalFile.VERSION_FILE, FileMode.Create);
            byte[] data = Encoding.UTF8.GetBytes(versions.ToString());
            stream.Write(data, 0, data.Length);
            stream.Flush();
            stream.Close();
        }

        /// <summary>
        /// 覆盖本地文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="data"></param>
        public static void ReplaceLocalRes(string filePath, byte[] data)
        {
            if(File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            FileStream stream = new FileStream(filePath, FileMode.Create);
            stream.Write(data, 0, data.Length);
            stream.Flush();
            stream.Close();
        }

        /// <summary>
        /// 判断路径是否存在
        /// </summary>
        /// <param name="path">路径+文件名</param>
        /// <returns></returns>
        public static bool IsExists(string path)
        {
            if(File.Exists(path))
            {
                return true;
            }
            return false;
        }
    }
}